package newPan3d.content.jineng
{
	import _Pan3D.core.MathTween;
	import _Pan3D.display3D.Display3DSprite;
	import _Pan3D.display3D.analysis.AnalysisServer;
	import _Pan3D.load.LoadInfo;
	import _Pan3D.load.LoadManager;
	import _Pan3D.texture.TextureManager;
	
	import _me.Scene_data;
	
	import flash.display3D.Context3D;
	import flash.display3D.Context3DBlendFactor;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	import flash.geom.Vector3D;

	public class HuoQiuDisplay3DSprite extends Display3DSprite
	{
		protected var lizhiVertexBuffer:VertexBuffer3D;
		protected var lizhiStateVertexBuffer:VertexBuffer3D;
		protected var lizhiIndexBuffer:IndexBuffer3D;
		protected var _program3D:Program3D;
		protected var _timer:Number=0;
		private var _startVector3D:Vector3D;
		private var _endVector3D:Vector3D;
		private var _inRender:Boolean=false;
		private var _loadArr:Vector.<Vector3D>;
		private var _num:int=10;

		public function HuoQiuDisplay3DSprite(context:Context3D)
		{
			super(context);
		}

		public function setMoveData(_P0:Vector3D,_P1:Vector3D):void
		{
			if(_inRender)return;
			
			var xx=_P1.x-_P0.x
			var yy=_P1.y-_P0.y
				
			var cc:Number=Math.sqrt(xx*xx+yy*yy)
				if(cc<40)return;
			
			_startVector3D=new Vector3D(_P0.x,_P0.y,_P0.z)
			_endVector3D=new Vector3D(_P1.x,_P1.y,_P1.z)
			_loadArr=new Vector.<Vector3D>;
			_timer=0;
			for (var i:int=0; i < _num; i++)
			{
				_loadArr.push(new Vector3D(_startVector3D.x, _startVector3D.y, _startVector3D.z));
			}
			_inRender=true;
		}
		private function getPosVertex( _tim:int):Vector3D
		{
			var a:int=_tim;
			if (a < 0)
			{
				a=0;
			}
			if (a > 20)
			{
				a=20;
			}
			
			_endVector3D.x=Scene_data.focus3D.x
			_endVector3D.z=Scene_data.focus3D.z
			
			var t:Number=MathTween.easeInOut(a, 0, 1, 20);
			var xx:Number=_startVector3D.x+t*(_endVector3D.x-_startVector3D.x);
			var zz:Number=_startVector3D.z+t*(_endVector3D.z-_startVector3D.z);
			return new Vector3D(xx, 0, zz);
		}

		override public function update():void
		{
			_context.setBlendFactors(Context3DBlendFactor.ONE, Context3DBlendFactor.ONE);
			if (!this._visible)
			{
				return;
			}
			if (!_inRender)
			{
				return;
			}
			if(_timer>30){  
				_inRender=false;
			}
			_timer++
				
			if (_objData && _objData.texture)
			{
				if (_program3D)
				{
					_context.setProgram(_program3D);
				}
				else
				{
					_context.setProgram(this.program);
				}
				setVc();
				setVa();
				resetVa();
			}
		}

		override protected function uplodToGpu():void
		{


			var vv:Vector.<Number>=new Vector.<Number>;
			var uu:Vector.<Number>=new Vector.<Number>;
			var ss:Vector.<Number>=new Vector.<Number>; //要确定每组成几个数据;暂为3跟顶点一样
			var ii:Vector.<uint>=new Vector.<uint>;
			var inum:int=_objData.vertices.length / 3;
			var xposition:Number;
			var yposition:Number;
			var zposition:Number;
			var wposition:Number;
			var angle:int=0;
			_loadArr=new Vector.<Vector3D>;
			for (var i:int; i < 10; i++)
			{
				vv=vv.concat(_objData.vertices);
				uu=uu.concat(_objData.uvs);
				xposition=i + 8; //存放VC号，
				yposition=((10 - i) / 5) * 0.5 + 0.2; //存放大小;
				zposition=0;
				wposition=1;
				for (var k:int=0; k < inum; k++)
				{
					ss.push(xposition, yposition, zposition, wposition); // 倾斜
				}
				for (var j:int=0; j < _objData.indexs.length; j++)
				{
					ii.push(_objData.indexs[j] + inum * i);
				}

				_loadArr.push(new Vector3D());

			}
			_objData.vertexBuffer=this._context.createVertexBuffer(vv.length / 3, 3);
			_objData.vertexBuffer.uploadFromVector(Vector.<Number>(vv), 0, vv.length / 3);

			_stateBuffer=this._context.createVertexBuffer(ss.length / 4, 4); //存状态;
			_stateBuffer.uploadFromVector(Vector.<Number>(ss), 0, ss.length / 4);

			_objData.uvBuffer=this._context.createVertexBuffer(uu.length / 2, 2);
			_objData.uvBuffer.uploadFromVector(Vector.<Number>(uu), 0, uu.length / 2);

			_objData.indexBuffer=this._context.createIndexBuffer(ii.length);
			_objData.indexBuffer.uploadFromVector(Vector.<uint>(ii), 0, ii.length);



		}

		override public function set url(value:String):void
		{
			_url=value;
			var loaderinfo:LoadInfo=new LoadInfo(Scene_data.fileRoot + "build/obj/" + _url + ".obj", LoadInfo.XML, onObjLoad, false);
			LoadManager.getInstance().addSingleLoad(loaderinfo);
		}

		override protected function onObjLoad(str:String):void
		{
			_objData=AnalysisServer.getInstance().analysisObj(str);
			TextureManager.getInstance().addTexture(Scene_data.fileRoot + "build/texture/" + _objData.mtl + ".jpg", addTexture, null);
		}

		override protected function setVa():void
		{
			_context.setVertexBufferAt(0, _objData.vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			_context.setVertexBufferAt(1, _objData.uvBuffer, 0, Context3DVertexBufferFormat.FLOAT_2);
			_context.setVertexBufferAt(2, _stateBuffer, 0, Context3DVertexBufferFormat.FLOAT_4);
			_context.setTextureAt(0, _objData.texture);
			_context.drawTriangles(_objData.indexBuffer, 0, -1);
		}
		private var _stateBuffer:VertexBuffer3D;

		override protected function resetVa():void
		{
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			_context.setVertexBufferAt(2, null);
			_context.setTextureAt(0, null);
		}

		override protected function setVc():void
		{
			this.updateMatrix();
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);


			_context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 8, Vector.<Number>([0, 0, 0, 1])); //这里暂可以传一个时间进去;
			

			for(var j:int=0;j<(10-1);j++){
				_loadArr[9-j]=new Vector3D(_loadArr[9-j-1].x,_loadArr[9-j-1].y,_loadArr[9-j-1].z);
			}
			var newpos:Vector3D=getPosVertex( _timer);
			_loadArr[0]=new Vector3D(newpos.x,newpos.y,newpos.z);
		
			for (var i:int=0; i < 10; i++)
			{
				 var pos:Vector3D=_loadArr[i];
				_context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, 8 + i, Vector.<Number>([pos.x, pos.y, pos.z, 1]));
	
			}

		}



		public function get program3D():Program3D
		{
			return _program3D;
		}

		public function set program3D(value:Program3D):void
		{
			_program3D=value;
		}

		public function get inRender():Boolean
		{
			return _inRender;
		}

		public function set inRender(value:Boolean):void
		{
			_inRender=value;
		}


	}
}
